package com.keepsport.keepsportapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.keepsport.keepsportapi.dao.UserIntegralDao;
import com.keepsport.keepsportapi.dao.UserIntegralLogDao;
import com.keepsport.keepsportapi.entity.User;
import com.keepsport.keepsportapi.entity.UserIntegral;
import com.keepsport.keepsportapi.entity.UserIntegralLog;
import com.keepsport.keepsportapi.entity.UserLog;
import com.keepsport.keepsportapi.service.intf.UserIntegralService;
import com.keepsport.keepsportapi.vo.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/***
 *
 *
 *                                                    __----~~~~~~~~~~~------___
 *                                   .  .   ~~//====......          __--~ ~~
 *                   -.            \_|//     |||\\  ~~~~~~::::... /~
 *                ___-==_       _-~o~  \/    |||  \\            _/~~-
 *        __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *    _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *  .~       .~       |   \\ -_    /  /-   /   ||      \   /
 * /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 * |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *          '         ~-|      /|    |-~\~~       __--~~
 *                      |-~~-_/ |    |   ~\_   _-~            /\
 *                           /  \     \__   \/~                \__
 *                       _--~ _/ | .-~~____--~-/                  ~~==.
 *                      ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                 -_     ~\      ~~---l__i__i__i--~~_/
 *                                 _-~-__   ~)  \--______________--~~
 *                               //.-~~~-~_--~- |-------~~~~~~~~
 *                                      //.-~~~--\
 *                               神兽保佑
 *                              代码无BUG!
 *
 @Author YK
 @create 2022/7/1 20:13
 */
@Service
public class UserIntegralServiceImpl implements UserIntegralService {
    @Resource
    private UserIntegralDao dao;
    @Resource
    private UserIntegralLogDao logDao;

    @Override
    public R signIn(int token) {
        //验证是否在线
        if (token>0) {
            QueryWrapper queryWrapper = new QueryWrapper<UserIntegral>();
            queryWrapper.eq("uid",token);
            UserIntegral userIntegral = dao.selectOne(queryWrapper);
            //验证用户是否第一次打卡
            if (userIntegral==null) {
                UserIntegral userIntegral1 = new UserIntegral(token, 50L, 0);
                if ( dao.insert(userIntegral1) > 0) {
                    UserIntegralLog userIntegralLog = new UserIntegralLog(token,50,1);
                    //记录积分或等级变动日志
                    logDao.insert(userIntegralLog);
                    return R.ok("恭喜你签到成功! 积分+50");
                    //insertuserIntegralLog(token);
                }
            } else {
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                if (!sdf.format(new Date()).equals(sdf.format(userIntegral.getCtime()))) {
                    userIntegral.setUid(token);
                    userIntegral.setCtime(new Date());
                    long integral = userIntegral.getIntegral();
                    integral += 50;
                    userIntegral.setIntegral(integral);
                    if (integral >= 100) {
                        userIntegral.setGrade(1);
                    } else if (integral >= 1000) {
                        userIntegral.setGrade(2);
                    }  else if (integral >= 3000) {
                        userIntegral.setGrade(3);
                    }  else if (integral >= 5000) {
                        userIntegral.setGrade(4);
                    }  else if (integral >= 10000) {
                        userIntegral.setGrade(5);
                    }

                    if ( dao.updateById(userIntegral) > 0) {
                        UserIntegralLog userIntegralLog = new UserIntegralLog(token,50,1);
                        //记录积分或等级变动日志
                        logDao.insert(userIntegralLog);
                        return R.ok();
                        //insertuserIntegralLog(token);
                    }

                }
            }

        }
        return R.fail("请勿重复签到！");
    }

    //查询积分
    @Override
    public R selectSign(int token) {
        //判断用户是否登录
        if (token>0) {
            if (dao.selectSign(token)!=null) {
//                Map<String,Integer> integers = dao.selectSign(token);
//                System.out.println(integers);
                return R.ok(dao.selectSign(token));
            } return R.ok(0);
        }
        return null;
    }

//    private R insertuserIntegralLog(int token) {
//        UserIntegralLog userIntegralLog = new UserIntegralLog(token,50,1);
//        //记录积分或等级变动日志
//        logDao.insert(userIntegralLog);
//        return R.ok();
//    }
}
